"""Copyright 2011 Thomas Anesta, Jonathan Brenner, Tyler Moylat, Rosa Tung, Sean Kim"""

"""

This file is part of Bubbles

Bubbles is free software: you can redistribute it and/or modify
    it under the terms of the GNU Lesser General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Bubbles is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU Lesser General Public License for more details.

    You should have received a copy of the GNU Lesser General Public License
    along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

"""

import pygame, sys, os
import BubblesGame
import random

#define the string we will use as our parameter delimiter
STR_DELIM = "%@%"

#define the constants that will signify the file locations

INTRODUCTION_FILE = "INTRO_FILE"
ENEMY_FILE = "ENEMY_FILE"
FILE_END = "END_INPUT"

#get your file location for game params
gameparamsloc = "textparams/gameparams.txt"#raw_input("Game Params File: ")

#check your file
if not os.path.isfile(gameparamsloc):
    print "FILE DOES NOT EXIST"
    sys.exit(1)

#try to read the file of parameters
try:
    gameparams = open(gameparamsloc).readlines()

except IOError as e:
    print "ERROR READING FROM FILE"
    sys.exit(1)

introfile = ""
enemyfile = ""

#now determine what is what
for line in gameparams:
    strings = line.split(STR_DELIM)
    if(strings[0] == INTRODUCTION_FILE):
        introfile = strings[1].rstrip()#should be nothing else there
    elif (strings[0] == ENEMY_FILE):
        enemyfile = strings[1].rstrip()#should be nothing else there
    elif (strings[0] == FILE_END):
        break
    else:
        break

#do we want to include font with the text file?
        
#get the text input
if not os.path.isfile(introfile):
    print "ERROR FINDING INTRO FILE"
    sys.exit(1)

#get our intro
introlist = []
try:
    thisfile = open(introfile)
    introlist = thisfile.readlines()

except IOError as e:
    print "ERROR READING INTRO FILE"
    sys.exit(1)

#nline="\n"
#introtext = nline.join(introlist)#joins the list together with newline characters
#pygame cannot render multiline text, do it one line at a time

if not os.path.isfile(enemyfile):
    print "ERROR FINDING ENEMY FILE"
    sys.exit(1)

enemylist = []
try:
    thisfile = open(enemyfile)
    enemylist = thisfile.readlines()

except IOError as e:
    print "ERROR READING ENEMY FILE"
    sys.exit()

for i, string in enumerate(introlist):
    string = string.rstrip()
    introlist[i] = string
    
for i, string in enumerate(enemylist):
    string = string.rstrip()
    string = string.split(STR_DELIM)
    enemylist[i] = string

random.seed()
g = BubblesGame.Game()
pygame.event.get()#clear the buffer
g.scroll_intro(introlist)
g.populate_waves(enemylist)

while True:
    g.process_events()
    g.update()
    g.draw()
    pygame.display.flip()
    g.next_frame()
    
    
